package sangpil.utils.pannel
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	import gs.easing.Linear;
	import gs.easing.Sine;
	
	import sangpil.utils.pannel.events.PageChangerEvent;
	[Event(name="MOVING_CHANGED", type = "sangpil.utils.pannel.events.PageChangerEvent")]
	public class PageChanger extends Sprite
	{
		private var firstMc:DisplayObject;
		private var secondMc:DisplayObject;
		private var flipDepth:uint = 100;
		private var count:uint = 0;
		
		public function changePage(firstPage:DisplayObject, secondPage:DisplayObject, direction:String):void
		{
			this.firstMc = firstPage;
			this.secondMc = secondPage;
			
			this.firstMc.alpha = 1;
			this.secondMc.alpha = 1;
			
			trace(direction);
			switch(direction)
			{
				case ChangerDirection.SWEEP:
					this.firstMc.visible = false;
					this.dispatchEvent(new PageChangerEvent(PageChangerEvent.MOVING_CHANGED));
				break;
				case ChangerDirection.RIGHT:
					this.secondMc.x = 0 - this.secondMc.width;
					TweenLite.to(this.firstMc, 0.5,{alpha:0, x:this.secondMc.width, ease:Cubic.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
					TweenLite.to(this.secondMc, 0.5, {alpha:1, x:0, ease:Cubic.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
				break;
				case ChangerDirection.LEFT:
					this.secondMc.x = 0 + this.secondMc.width;
					TweenLite.to(this.firstMc, 0.5,{alpha:0, x:-1*this.secondMc.width, ease:Cubic.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
					TweenLite.to(this.secondMc, 0.5, {alpha:1, x:0, ease:Cubic.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
				break;
				case ChangerDirection.DIMED:
					this.secondMc.alpha = 0;
					TweenLite.to(this.firstMc, 0.5,{alpha:0, ease:Sine.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
					TweenLite.to(this.secondMc, 0.5, {alpha:1, ease:Sine.easeOut, delay:0, onComplete:onFinishTween, onCompleteParams:[1]});
				break;
				case ChangerDirection.FLIP_RIGHT:
					this.secondMc.alpha = 0;
					TweenLite.to(this.firstMc, 0.3,{alpha:1, x:this.firstMc.width/2, z:this.firstMc.width+this.flipDepth, rotationY:90, ease:Linear.easeNone, delay:0, onComplete:middleFlipTween, onCompleteParams:[ChangerDirection.FLIP_RIGHT]});
				break;
				case ChangerDirection.FLIP_LEFT:
					this.secondMc.alpha = 0;
					TweenLite.to(this.firstMc, 0.3,{alpha:1, x:this.firstMc.width/2, z:this.flipDepth, rotationY:-90, ease:Linear.easeNone, delay:0, onComplete:middleFlipTween, onCompleteParams:[ChangerDirection.FLIP_LEFT]});
				break;
			}
			
			
		}
		
		private function onFinishTween(cnt:uint):void
		{
			this.count += cnt;
			if(this.count >= 2){
				this.dispatchEvent(new PageChangerEvent(PageChangerEvent.MOVING_CHANGED));
				
				this.firstMc.alpha = 0;
				this.firstMc.x = this.firstMc.y = this.firstMc.z = this.firstMc.rotationY = 0;
				this.count = 0;
			}
		}
		
		private function middleFlipTween(direction:String):void
		{
			switch(direction)
			{
				case ChangerDirection.FLIP_RIGHT:
					this.secondMc.alpha = 1;
					this.secondMc.z = 0+this.flipDepth;
					this.secondMc.x = this.firstMc.x;
					this.secondMc.rotationY = 90*-1;
					
					TweenLite.to(this.secondMc, 0.3,{alpha:1, x:0, z:0, rotationY:0, ease:Linear.easeNone, delay:0, onComplete:onFinishTween, onCompleteParams:[2]});
				break;
				case ChangerDirection.FLIP_LEFT:
					this.secondMc.alpha = 1;
					this.secondMc.z = this.secondMc.width+this.flipDepth;
					this.secondMc.x = this.firstMc.x;
					this.secondMc.rotationY = 90;
					
					TweenLite.to(this.secondMc, 0.3,{alpha:1, x:0, z:0, rotationY:0, ease:Linear.easeNone, delay:0, onComplete:onFinishTween, onCompleteParams:[2]});
				break;
			}
		}
		
		public function PageChanger()
		{
			super();
		}
	}
}